package com.sky.controller.admin;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersStatusAllDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import static com.sky.entity.Orders.*;

/**
 * @author yangkexuan
 * @className OrderController
 * @date 2023/6/14 17:50
 * @description
 */
@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "用户端-订单接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 取消订单
      * @return null
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result<String> cancelOrder(@RequestBody OrdersStatusAllDTO order){
        // 6:已取消
        order.setStatus(CANCELLED);
        order.setCancelTime(LocalDateTime.now());
        orderService.updateCancelOrder(order);
        return Result.success();
    }

    /**
     * 完成订单
     * @param {id}
     * @return null
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result<String> completeOrder(@PathVariable Integer id){
        OrdersStatusAllDTO order = new OrdersStatusAllDTO();
        order.setId(Long.valueOf(id));
        // 5 已完成
        order.setStatus(COMPLETED);
        orderService.updateCancelOrder(order);
        return Result.success();
    }

    /**
     * 拒单
     * @return null
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result<String> rejectionOrder(@RequestBody OrdersStatusAllDTO order){
        // TODO 取消订单+退款（这个没写）
        // 6 已取消
        order.setStatus(7);
        order.setCancelTime(LocalDateTime.now());
        orderService.updateCancelOrder(order);
        return Result.success();
    }

    /**
     * 接单
     * @return null
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result<String> confirmOrder(@RequestBody OrdersStatusAllDTO order){
        // 3 已接单
        order.setStatus(CONFIRMED);
        orderService.updateCancelOrder(order);
        return Result.success();
    }

    /**
     * 派送订单
     * @param {id}
     * @return null
     */
    @PutMapping("delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> confirmOrder(@PathVariable Integer id){
        OrdersStatusAllDTO order = new OrdersStatusAllDTO();
        order.setId(Long.valueOf(id));
        //4 派送中
        order.setStatus(DELIVERY_IN_PROGRESS);
        orderService.updateCancelOrder(order);
        return Result.success();
    }

    /**
     * 各个状态的订单数量统计
     * @return null
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> orderStatistics(){
        OrderStatisticsVO orderStatisticsVO = orderService.selectStatistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 查询订单详情
     * @param {id}
     * @return null
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详细")
    public Result<OrderVO> detailsOrder(@PathVariable Long id){
        return Result.success(orderService.selectOrderDetailById(id));
    }

    /**
     * 订单搜索
      * @return null
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单查询")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        PageResult pageResult = orderService.page(ordersPageQueryDTO);
        return Result.success(pageResult);
    }
}
